import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/widgets/comm_app_bar.dart';
import 'package:baselib/widgets/comm_click_widget.dart';
import 'package:baselib/widgets/gradient_card_view.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/comm_widget_manager.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/get.dart';
import 'package:module_business/module_my/viewmodel/my_view_model.dart';
import 'package:module_business/module_my/viewmodel/other_person_home_view_model.dart';

/**
 * 其他个人信息主页主页
 */
class OtherPersonHomePage extends GetWidget<OtherPersonHomeViewModel> {
  @override
  Widget build(BuildContext context) {
    Get.put(OtherPersonHomeViewModel());
    return PageWidget.build(builder: (OtherPersonHomeViewModel model) {
      return PageWidget.PageScaffoldWidget(
        isShowBg: true,
        // appBar: CommAppBar(
        //   leading: SizedBox(width: PxUtil.instance.px_0,),
        //   backgroundColor: ColorUtil.instance.color_transparent,
        // ),

        body: RefreshUtil.init(
          model.refreshController,
          () => model.onRefresh(pageCur: model?.page = 1),
          () => model.onRefresh(pageCur: model?.page + 1),
          _buildListUi(model),
          physics: ClampingScrollPhysics(),
          loadStatus: model?.loadStatus,
          enablePullDown: true,
          enablePullUp: false,
          isCanMoreData: model?.isCanMoreData ?? true,
          onReFreshFunction: () => model.onRefresh(pageCur: model?.page = 1),
        ),
      );
    });
  }

  Widget _buildListUi(OtherPersonHomeViewModel model) {
    LogUtil.d('giftsDatas--->${model.giftsDatas?.length}');
    return ListView(
      shrinkWrap: true,
      children: [
        //顶部个人信息
        _buildTopPersonInfoUi(model),
        SizedBox(
          height: PxUtil.instance.px_18,
        ),
        //fans、follow、friends、visitor
        _buildFansFoUi(model),
        SizedBox(
          height: PxUtil.instance.px_20,
        ),
        Container(
          padding: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_12,
            bottom: PxUtil.instance.px_10,
          ),
          child: Text(
            StringUtil.instance.Personal_Profile,
            style: TextStyle(
                color: ColorUtil.instance.color_white,
                fontWeight: FontWeight.w500,
                fontSize: PxUtil.instance.px_16),
          ),
        ),
        //功能列表
        Container(
          margin: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_15,
          ),
          padding: EdgeInsets.only(
            left: PxUtil.instance.px_13,
            right: PxUtil.instance.px_13,
            top: PxUtil.instance.px_16,
            bottom: PxUtil.instance.px_16,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(PxUtil.instance.px_8),
            color: ColorUtil.instance.color_394676,
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  GradientCardView(
                      width: PxUtil.instance.px_4,
                      height: PxUtil.instance.px_15,
                      elevation: PxUtil.instance.px_0,
                      margin: EdgeInsets.only(),
                      padding: EdgeInsets.only(),
                      child: Container()),
                  SizedBox(
                    width: PxUtil.instance.px_5,
                  ),
                  Text(
                    StringUtil.instance.Personal_signature,
                    style: TextStyle(
                        color: ColorUtil.instance.color_white,
                        fontWeight: FontWeight.w500,
                        fontSize: PxUtil.instance.px_14),
                  ),
                ],
              ),
              SizedBox(
                height: PxUtil.instance.px_8,
              ),
              Text(
                '${model.myInfo?.signature ?? 'Hi！My name is ...'}',
                style: TextStyle(
                    color: ColorUtil.instance.color_half_white,
                    fontWeight: FontWeight.w500,
                    fontSize: PxUtil.instance.px_14),
              ),
              SizedBox(
                height: PxUtil.instance.px_24,
              ),
              //Gift wall
              Row(
                children: [
                  GradientCardView(
                      width: PxUtil.instance.px_4,
                      height: PxUtil.instance.px_15,
                      elevation: PxUtil.instance.px_0,
                      margin: EdgeInsets.only(),
                      padding: EdgeInsets.only(),
                      child: Container()),
                  SizedBox(
                    width: PxUtil.instance.px_5,
                  ),
                  Text(
                    StringUtil.instance.Gift_wall,
                    style: TextStyle(
                        color: ColorUtil.instance.color_white,
                        fontWeight: FontWeight.w500,
                        fontSize: PxUtil.instance.px_14),
                  ),
                ],
              ),
              SizedBox(
                height: PxUtil.instance.px_20,
              ),
              GridView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.only(
                    // top: PxUtil.instance.px_20,
                    bottom: PxUtil.instance.px_20,
                    left: PxUtil.instance.px_15,
                    right: PxUtil.instance.px_15,
                  ),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 4,
                      mainAxisSpacing: 20,
                      crossAxisSpacing: 20,
                      childAspectRatio: 1/1.45),
                  itemCount: model.giftsDatas?.length??0,
                  itemBuilder: (BuildContext context, int index) {
                    var giftsData = model.giftsDatas[index];
                    LogUtil.d('giftsDatas--->${model.giftsDatas?.length}');
                    return Container(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          ImagesUtil.loadNetworkImage(
                              giftsData?.giftSmallImg??''
                          ),
                          SizedBox(height: PxUtil.instance.px_5,),
                          Text(
                            'x${giftsData?.giftPrice??0}',
                            style: TextStyle(
                              color: ColorUtil.instance.color_half_white,
                              fontSize: PxUtil.instance.px_12
                            ),
                          )
                        ],
                      ),
                    );
                  }),
            ],
          ),
        ),

        //Log Out、Delete Account
        model.isMySelf()?SizedBox(width: PxUtil.instance.px_0,):
        Container(
          margin: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_15,
            top: PxUtil.instance.px_21,
            bottom: PxUtil.instance.px_21,
          ),
          child: Row(
            children: [
              Expanded(
                  child: InkWellButton(
                    onPressed: () => model.doFollow(),
                    child: GradientCardView(
                      elevation: PxUtil.instance.px_0,
                      gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            ColorUtil.instance.color_26F1EB,
                            ColorUtil.instance.color_2075FC,
                          ]),
                      padding: EdgeInsets.only(
                        left: PxUtil.instance.px_22,
                        right: PxUtil.instance.px_22,
                        top: PxUtil.instance.px_13,
                        bottom: PxUtil.instance.px_13,
                      ),
                      borderRadius: BorderRadius.circular(PxUtil.instance.px_26),
                      child: Center(
                        child: Text(
                          model.isInFollows()?StringUtil.instance.Unfollow:StringUtil.instance.Follow,
                          style: TextStyle(
                              color: ColorUtil.instance.color_white,
                              fontSize: PxUtil.instance.px_14),
                        ),
                      ),
                    ),
                  )),
              SizedBox(
                width: PxUtil.instance.px_15,
              ),
              Expanded(
                  child: InkWellButton(
                    onPressed: () => NavigatorUtil.goBack(),
                    child: GradientCardView(
                      elevation: PxUtil.instance.px_0,
                      gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            ColorUtil.instance.color_7B05FE,
                            ColorUtil.instance.color_BC43FF,
                          ]),
                      padding: EdgeInsets.only(
                        left: PxUtil.instance.px_22,
                        right: PxUtil.instance.px_22,
                        top: PxUtil.instance.px_13,
                        bottom: PxUtil.instance.px_13,
                      ),
                      borderRadius: BorderRadius.circular(PxUtil.instance.px_26),
                      child: Center(
                        child: Text(
                          StringUtil.instance.Say_Hi,
                          style: TextStyle(
                              color: ColorUtil.instance.color_white,
                              fontSize: PxUtil.instance.px_14),
                        ),
                      ),
                    ),
                  )),
                  SizedBox(width: PxUtil.instance.px_24,),
                  InkWellButton(
                    onPressed: ()=>model.popPoint(),
                    child: ImagesUtil.loadAssetImage(
                        DrawableUtil.instance.icon_right_out_img,
                        width: PxUtil.instance.px_44,
                        fit: BoxFit.fitWidth
                    ),
                  )
            ],
          ),
        ),
        SizedBox(
          height: PxUtil.instance.px_15,
        ),
      ],
    );
  }

  _buildTopPersonInfoUi(OtherPersonHomeViewModel model) {
    return Container(
      child: Stack(
        children: [
          ImagesUtil.loadAssetImage(DrawableUtil.instance.icon_person_top_bg,
              width: PxUtil.instance.screenWidth, fit: BoxFit.fitWidth),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: PxUtil.instance.statusBarHeight + PxUtil.instance.px_10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Expanded(
                      child: Row(
                    children: [
                      InkWellButton(
                        onPressed: () => NavigatorUtil.goBack(),
                        child: Container(
                          margin: EdgeInsets.only(left: PxUtil.instance.px_17),
                          child: ImagesUtil.loadAssetImage(
                              DrawableUtil.instance.icon_arrow_left_white,
                              height: PxUtil.instance.px_18),
                        ),
                      )
                    ],
                  )),
                  model.isMySelf()?SizedBox(width: PxUtil.instance.px_0,):
                  InkWellButton(
                    onPressed: ()=>model.popPoint(),
                    child: Container(
                      margin: EdgeInsets.only(right: PxUtil.instance.px_17),
                      child: ImagesUtil.loadAssetImage(
                          DrawableUtil.instance.icon_other_person_point,
                          width: PxUtil.instance.px_18),
                    ),
                  )
                ],
              ),
              SizedBox(
                height: PxUtil.instance.screenHeight / 56,
              ),
              //头像
              CommWidgetManager.buildCicleImagWidget(
                model.myInfo?.icon ?? '',
                placeholder: DrawableUtil.instance.icon_default_head,
                width: PxUtil.instance.px_60,
                height: PxUtil.instance.px_60,
                roundRadius: PxUtil.instance.px_30,
                backgroundColor: ColorUtil.instance.color_f2f2f2,
                padding: EdgeInsets.only(),
                margin: EdgeInsets.only(),
              ),
              SizedBox(
                height: PxUtil.instance.px_10,
              ),
              //名称
              Text(
                model.myInfo?.nickname ?? '',
                style: TextStyle(
                    color: ColorUtil.instance.color_white,
                    fontSize: PxUtil.instance.px_18,
                    fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: PxUtil.instance.px_5,
              ),
              //ID
              Text(
                '${StringUtil.instance.ID} :${model.myInfo?.userId ?? 0}',
                style: TextStyle(
                    color: ColorUtil.instance.color_half_white,
                    fontSize: PxUtil.instance.px_14,
                    fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: PxUtil.instance.px_12,
              ),
              //爱好
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    decoration: BoxDecoration(
                        color: ColorUtil.instance.color_FC45BB,
                        borderRadius:
                            BorderRadius.circular(PxUtil.instance.px_14)),
                    padding: EdgeInsets.only(
                      left: PxUtil.instance.px_15,
                      right: PxUtil.instance.px_15,
                      top: PxUtil.instance.px_3,
                      bottom: PxUtil.instance.px_3,
                    ),
                    child: Text(
                      'Sport',
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14,
                          fontWeight: FontWeight.w500),
                    ),
                  ),
                  SizedBox(
                    width: PxUtil.instance.px_15,
                  ),
                  Container(
                    decoration: BoxDecoration(
                        color: ColorUtil.instance.color_87D660,
                        borderRadius:
                            BorderRadius.circular(PxUtil.instance.px_14)),
                    padding: EdgeInsets.only(
                      left: PxUtil.instance.px_15,
                      right: PxUtil.instance.px_15,
                      top: PxUtil.instance.px_3,
                      bottom: PxUtil.instance.px_3,
                    ),
                    child: Text(
                      'Yogo',
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14,
                          fontWeight: FontWeight.w500),
                    ),
                  )
                ],
              )
            ],
          )
        ],
      ),
    );
  }

  _buildFansFoUi(OtherPersonHomeViewModel model) {
    return GradientCardView(
      elevation: PxUtil.instance.px_0,
      margin: EdgeInsets.only(
        left: PxUtil.instance.px_15,
        right: PxUtil.instance.px_15,
      ),
      alignment: Alignment.center,
      borderRadius: BorderRadius.circular(PxUtil.instance.px_8),
      gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            ColorUtil.instance.color_435688,
            ColorUtil.instance.color_435688,
          ]),
      backGroundColor: ColorUtil.instance.color_435688,
      child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: ColorUtil.instance.color_435688,
            borderRadius: BorderRadius.circular(PxUtil.instance.px_8),
          ),
          child: Row(
            children: [
              _buildFansFoItemUi(
                  model, StringUtil.instance.Fans, model.myInfo?.fansNum ?? 0),
              _buildFansFoItemUi(model, StringUtil.instance.Follow,
                  model.myInfo?.followNum ?? 0),
              _buildFansFoItemUi(model, StringUtil.instance.Friends,
                  model.myInfo?.friendNum ?? 0),
              // _buildFansFoItemUi(model,StringUtil.instance.Visitor,model.myInfo?.visitorNum??0),
            ],
          )),
    );
  }

  _buildFansFoItemUi(OtherPersonHomeViewModel model, String tag, int num) {
    return Expanded(
        child: InkWellButton(
      onPressed: () => {},
      child: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(
              height: PxUtil.instance.px_12,
            ),
            Text(
              '${num ?? 0}',
              style: TextStyle(
                  color: ColorUtil.instance.color_white,
                  fontSize: PxUtil.instance.px_16,
                  fontWeight: FontWeight.bold),
            ),
            SizedBox(
              height: PxUtil.instance.px_5,
            ),
            Text(
              tag,
              style: TextStyle(
                  color: ColorUtil.instance.color_half_white,
                  fontSize: PxUtil.instance.px_13,
                  fontWeight: FontWeight.w500),
            ),
            SizedBox(
              height: PxUtil.instance.px_12,
            ),
          ],
        ),
      ),
    ));
  }
}
